<script setup lang="ts">
import {ref, onMounted, computed} from 'vue'
import {useRoute} from 'vue-router'
import { fetchProductDetail} from "../request/req.ts";
import { ElNotification} from "element-plus";
import {useCartStore} from "../stores/cart.ts";
import {formatPrice} from "../util/formatPrice.ts";
import Imgae from '../assets/iamge.svg'
import {useLogoTextStore} from '../stores/logoText.ts'

const logoTextStore = useLogoTextStore()
const image = logoTextStore.getItemById(3)
const cart = useCartStore()
const route = useRoute()
const product = ref({
  id: route.query.id ?? 1001,
  title: '無商品',
  price: 0,
  rating: 0,
  reviews: 0,
  weight: '',
  grade: '',
  shelfLife: '',
  storage: '',
  summary:
      '',
  image: Imgae
})

const Image = logoTextStore.getItemById(3)
// 封面
const qty = ref(1)

// 将后端 HTML 描述中图片路径补全
const descHtml = computed(() => {
  const raw = product.value.summary || ''
  return raw.replace(/src=\"(?!https?:)(\/[^\"]+)\"/g, (_m, p1) => `src=\"${Image?.text}${p1}\"`)
})

//获取详情数据
const getDetail = async () => {
  const res = await fetchProductDetail({Id: route.params.id})
  const item = res.ShowGoods
  product.value = {
    id: item.Id,
    title: item.GoodsName,
    price: item.Pirce,
    rating: item.StarCount ? item.StarCount / 2 : 0,
    reviews: item.Views || 0,
    weight: item.Format || '250g',
    grade: '特级',
    shelfLife: '18个月',
    storage: '阴凉干燥处，密封保存',
    summary: item.ProductDescription || '暂无商品描述',
    image: image?.text + item.PicUrls
  }
}

// 添加到购物车
const addToCart = (product: any) => {
  cart.add({
    id: product.id,
    title: product.title,
    image: product.image,
    price: product.price,
    qty: 1,
  })
  ElNotification({
    title: `商品:${product.title}添加成功`,
    type: 'success',
    showClose: false,
    position: 'bottom-right'
  })
}

onMounted(() => {
  getDetail()
})
</script>

<template>
  <div class="detail-page">
    <div class="top-card">
      <!-- 单张封面 -->
      <div class="gallery">
        <div class="main-img">
          <img :src="product.image" :alt="product.title"/>
        </div>
      </div>

      <!--信息 -->
      <div class="info">
        <h1 class="title">{{ product.title }}</h1>
        <div class="rate-row">
          <el-rate
              v-model="product.rating" disabled
              show-score
              text-color="#ff9900"
              score-template="{value}"/>
          <span class="reviews">({{ product.reviews }})</span>
        </div>
        <div class="specs">
          <div class="row"><span class="label">净含量：</span><span class="product-text">{{ product.weight }}</span></div>
          <div class="row"><span class="label">瀏覽次數：</span><span class="product-text">{{ product.reviews }}</span></div>
        </div>

        <div class="price">HK$ {{ formatPrice(product.price) }}  元</div>

        <div class="buy-row">
          <div class="qty">
            <el-input-number v-model="qty" :min="1" :max="99" size="large"/>
          </div>
          <el-button type="primary" size="large" @click="addToCart(product)">加入購物車</el-button>
        </div>
      </div>
    </div>

    <div class="desc-card">
      <div class="desc-responsive">
        <div class="desc-html" v-html="descHtml"></div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.detail-page {
  max-width: 1200px;
  margin: 0 auto;
  padding: 16px;
}
.product-text {
  color: rgba(232, 232, 232, 0.8);
}
.top-card {
  background-color: #2c3e50;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, .08);
  display: grid;
  grid-template-columns: 1.1fr 1fr;
  gap: 24px;
  padding: 16px;
}

.gallery {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.main-img {
  width: 100%;
  aspect-ratio: 4/3;
  background: #fafafa;
  border-radius: 10px;
  overflow: hidden;
}

.main-img img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.info {
  padding: 6px 2px;
}

.title {
  margin: 0 0 8px;
  font-size: 24px;
  font-weight: 800;
  color: rgba(207, 207, 207, 0.84);
}

.rate-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.reviews {
  color: #d1d1d1;
  font-size: 13px;
}

.specs {
  display: grid;
  gap: 6px;
  margin: 10px 0 14px;
}

.row .label {
  color: rgba(219, 219, 219, 0.88);
  font-weight: bold;
}

.price {
  color: rgba(234, 234, 234, 0.88);
  font-weight: 800;
  font-size: 24px;
  margin: 10px 0 14px;
}

.buy-row {
  display: flex;
  align-items: center;
  gap: 12px;
}

.qty {
  display: flex;
  align-items: center;
}

.desc-card {
  background: #2c3e50;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, .08);
  margin-top: 18px;
  padding: 16px;
}

.desc-responsive {
  width: 100%;
  overflow-x: hidden;
  word-wrap: break-word;
  overflow-wrap: anywhere;
}

.desc-responsive :deep(img) {
  max-width: 100% !important;
  height: auto !important;
  display: block;
}

.desc-responsive :deep(table) {
  width: 100% !important;
  border-collapse: collapse;
  display: block;
  overflow-x: auto;
}

.desc-responsive :deep(pre) {
  white-space: pre-wrap;
  word-break: break-word;
}

.desc-responsive :deep(iframe) {
  max-width: 100%;
}

.desc-card h2 { margin: 0 0 10px; font-size: 18px; }
.desc-html { color: #444; line-height: 1.8; font-size: 14px; }
.desc-html img { max-width: 100%; height: auto; display: block; }
.desc-html ul { padding-left: 18px; }
.desc-html p { margin: 8px 0; }

@media (max-width: 992px) {
  .top-card { grid-template-columns: 1fr; }
}

@media (max-width: 768px) {
  .detail-page {
    padding: 12px;
  }

  .top-card {
    padding: 12px;
    gap: 16px;
  }

  .title {
    font-size: 20px;
    margin-bottom: 6px;
  }

  .price {
    font-size: 20px;
    margin: 8px 0 12px;
  }

  .buy-row {
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
  }

  .qty {
    justify-content: center;
  }

  .desc-card {
    margin-top: 16px;
    padding: 12px;
  }

  .desc-responsive {
    overflow-x: auto;
  }

  .desc-html {
    font-size: 13px;
    line-height: 1.6;
  }

  .desc-html h2 {
    font-size: 16px;
    margin-bottom: 8px;
  }

  .desc-html img {
    max-width: 100%;
    height: auto;
    margin: 8px 0;
  }

  .desc-html ul {
    padding-left: 16px;
  }

  .desc-html p {
    margin: 6px 0;
  }
}
</style>
